package Lee_code;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class Solution {
    List<List<Integer>> res = new LinkedList<>();
    int len;

    public List<List<Integer>> permute(int[] nums) {
        len = nums.length;
        int[] visited = new int[len];
        List<Integer> list = new LinkedList<>();
        backtrack(list, nums, visited);
        return res;
    }

    private void backtrack(List<Integer> list, int[] nums, int[] visited) {
        if (list.size() == len) {
            res.add(new ArrayList<>(list));
        }
        for (int i = 0; i < nums.length; i++) {
            if (visited[i]==1){
                continue;
            }
            visited[i] = 1;
            list.add(nums[i]);
            backtrack(list, nums, visited);
            visited[i] = 0;
            list.remove(list.size()-1);
        }
    }
}